<?php
class Calendars extends Public_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index($slug='main')
	{
		$data['slug'] = $slug;
		$data['category'] = new Category;
		$data['category']->get_by_slug($slug);
		$this->template->build('calendar_index',$data);
	}
	
	function date($slug,$date)
	{
		$data['date'] = mysql_to_unix($date);
		$data['slug'] = $slug;
		$data['category'] = new Category;
		$data['category']->get_by_slug($slug);
		$this->template->build('calendar_index2',$data);
	}
	
	function view($id)
	{
		if(@$_GET)
		{
			$calendars = new Calendar($id);
			$calendars->where('start',$calendars->start)->get();
			foreach($calendars as $calendar)
			{
				echo anchor('calendars/view/'.$calendar->id,$calendar->title).'<br />';
			}
		}
		else
		{
			$type = array("e-blue" => "ประชุม","e-red" => "สัมมา","e-green" => "อบรม","e-violet" => "อื่นๆ");
			$data['calendar'] = new Calendar($id);
			$data['category'] = new Category($data['calendar']->agency_id);
			$data['slug'] = $data['category']->slug;
			$data['calendar']->counter();
			$data['type'] = $type[$data['calendar']->className];
			$this->template->build('calendar_view',$data);
		}
	}
	
	function events()
	{
		$calendar = New Calendar();
		//if((!empty($_GET['agency_id']))&&($_GET['agency_id']<>1)) $calendar->where('agency_id', $_GET['agency_id']);
		$where = '';
		if((!empty($_GET['agency_id']))&&($_GET['agency_id']<>1)) $where = ' where agency_id = '.$_GET['agency_id'];
		
		$sql = 'select calendars.id,concat(title," - ",content_categories.name) title,className,concat(start," ",start_time) start,concat(end," ",end_time) end,calendars.user_id
		from calendars
		left join content_categories
		on calendars.agency_id = content_categories.id 
		'.$where;
		$calendar->query($sql);
		foreach($calendar as $calendars):
		echo $calendars->user->username.br();
		endforeach;
		//$events = $calendar->all_to_array();
		//echo json_encode($events);
	}	
	
	function inc_side($slug='main')
	{
		$data['slug'] = $slug;
		$data['category'] = new Category;
		$data['category']->get_by_slug($slug);
		$prefs['template'] = '
   		{table_open}<table class="table-calendar">{/table_open}
   			
			{heading_row_start}<tr class="heading">{/heading_row_start}
   				{heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
   				{heading_title_cell}<th colspan="{colspan}"><a href="calendars/index/main" >{heading}</a></th>{/heading_title_cell}
   				{heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}
   			{heading_row_end}</tr>{/heading_row_end}
   			
			{week_row_start}<tr class="week">{/week_row_start}
   				{week_day_cell}<td>{week_day}</td>{/week_day_cell}
   			{week_row_end}</tr>{/week_row_end}
   		
			{cal_row_start}<tr>{/cal_row_start}
   				{cal_cell_start}<td>{/cal_cell_start}
   					{cal_cell_content}<a href="{content}" rel="{content}" title="กิจกรรม" >{day}</a>{/cal_cell_content}
   					{cal_cell_content_today}<div class="highlight"><a href="{content}" rel="{content}" title="กิจกรรม" >{day}</a></div>{/cal_cell_content_today}
   					{cal_cell_no_content}{day}{/cal_cell_no_content}
   					{cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}
   					{cal_cell_blank}&nbsp;{/cal_cell_blank}
   				{cal_cell_end}</td>{/cal_cell_end}
   			{cal_row_end}</tr>{/cal_row_end}
   		
		{table_close}</table>{/table_close}
		';
		$this->lang->load('calendar', 'thai');
		$this->load->library('calendar', $prefs);
		//$items = $this->db->getarray('select id,day(start) as c_date from calendars where year(start) = ? and month(start) = ?',array(date('Y'),date('m')));
		$calendars = new Calendar();
		if($slug<>'main') $calendars->where('agency_id', $data['category']->id);
		$calendars->select('calendars.id,day(start) as c_date')->where('year(start) = '.date('Y').' and month(start) = '.date('m'))->get();
		foreach($calendars as $calendar){$data[$calendar->c_date] = base_url().'calendars/date/'.$slug.'/'.date('Y-m-').$calendar->c_date;}
		$data['calendar'] = $this->calendar->generate(date('Y'), date('m'), @$data, TRUE);
		$this->load->view('calendar_side',$data);
	}
}
?>